package routers

import (
	"Article-Manage/controllers"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
)

func init() {
	beego.InsertFilter("/*", beego.BeforeExec, Filter)
	beego.Router("/", &controllers.UserController{}, "get:GetLogin")
	beego.Router("/register", &controllers.UserController{}, "get:GetRegister;post:PostRegister")
	beego.Router("/login", &controllers.UserController{}, "get:GetLogin;post:PostLogin")
	beego.Router("/articles", &controllers.ArticleController{}, "get:GetArticles")
	beego.Router("/addArticle", &controllers.ArticleController{}, "get:GetArticle;post:PostArticle")
	beego.Router("/articleDetail", &controllers.ArticleController{}, "get:GetArticleDetail")
	beego.Router("/articleEdit", &controllers.ArticleController{}, "get:GetArticleEdit;post:PostArticleEdit")
	beego.Router("/articleDelete", &controllers.ArticleController{}, "get:DeleteArticle")
	beego.Router("/type", &controllers.ArticleController{}, "get:GetType;post:PostType")
	beego.Router("/type/delete", &controllers.ArticleController{}, "get:DeleteType")
	beego.Router("/logout", &controllers.UserController{}, "get:Logout")
}

var Filter = func(ctx *context.Context) {
	userName := ctx.Input.Session("userName")
	if userName == nil && ctx.Request.RequestURI != "/login" && ctx.Request.RequestURI != "/register" {
		ctx.Redirect(302, "/login")
		return
	}
}
